package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void add(Dish dish) {
        //分别向菜品和菜品口味表中添加
//        System.out.println(dish.getName());
        dishMapper.insert(dish);
        if (CollectionUtil.isNotEmpty(dish.getFlavors())){
            for (DishFlavor dishFlavor : dish.getFlavors()) {
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public Page<Dish> findByPageAndName(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        Page<Dish> page = new Page<>(pageNum,pageSize);
        queryWrapper.like(StringUtils.isNoneEmpty(name),Dish::getName,name);
        List<Dish> dishList = dishMapper.selectList(queryWrapper);
        Page<Dish> pageResult = dishMapper.selectPage(page, queryWrapper);
        return pageResult;
    }

    @Override
    public Boolean delete(List<Long> ids) {
        //先判断一下，ids中的状态是否有起售
        List<Dish> dishes = dishMapper.findByIds(Dish.SALE,ids);
        if (CollectionUtil.isNotEmpty(dishes)){
            return false;
        }
        //删除口味表中的数据
        dishFlavorMapper.deleteByIds(ids);
        //删除菜品表中的数据
        dishMapper.deleteByIds(ids);
        return true;
    }

    //修改菜品状态
    @Override
    public void statusConvert(Integer status, List<Long> ids) {
        //使用mybatisplus
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        Dish dish = new Dish();
        dish.setStatus(status);
        dishMapper.update(dish,queryWrapper);
        //使用mybaits
        //dishMapper.statusConvert(status,ids);
    }

    @Override
    public void update(Dish dish) {
        dishMapper.updateById(dish);
        //重要的是口味表，先删除，再添加
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        int delete = dishFlavorMapper.delete(queryWrapper);
        if (CollectionUtil.isNotEmpty(dish.getFlavors())){
            for (DishFlavor dishFlavor : dish.getFlavors()) {
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public List<Dish> findByName(String name) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Dish::getName,name);
        List<Dish> dishList = dishMapper.selectList(queryWrapper);
        return dishList;
    }

    @Override
    public List<Dish> findWithType(Long categoryId, Integer status) {
        //根据两个参数查询setmeal,然后再查出菜品规格
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,categoryId)
                .eq(Dish::getStatus,status);
        List<Dish> dishs = dishMapper.selectList(queryWrapper);
        for (Dish dish : dishs) {
            LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(queryWrapper1);
            dish.setFlavors(dishFlavorList);
        }

        return dishs;
    }

    @Override
    public List<Dish> findByDish(Long categoryId) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,categoryId);
        List<Dish> dishList = dishMapper.selectList(queryWrapper);
        return dishList;
    }

    @Override
    public Dish findById(Long id) {
        //这里需要先查询dish表，然后 根据id查询口味，然后进行设置
        Dish dish = dishMapper.selectById(id);
        if (dish != null){
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,dish.getId());
            //查出菜品口味
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(queryWrapper);
            //查询菜品分类
            System.out.println(dish.getCategoryId());
            Category category = categoryMapper.selectById(dish.getCategoryId());
            dish.setFlavors(dishFlavorList);
            dish.setCategoryName(category.getName());
        }
        return dish;
    }
}
